<?php
/**
 * 用户智能选房消息通知模型
 */

namespace app\api\model;


use PDOStatement;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class MessageUserHouse extends Model {

    /**
     * 获取最新消息
     * @param $user_id
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getNew($user_id, $column) {
        $data = Db::name("message_user_house")->where("user_id", $user_id)->field($column)->order('itime', 'desc')->select();
        if (!empty($data)) {
            return $data[0];
        } else {
            return [];
        }
    }

    /**
     * 未读
     * @param $user_id
     * @return float|string
     */
    public static function getNotread($user_id) {
        return Db::name("message_user_house")->where("user_id", $user_id)->where("is_read", 0)->count();
    }

    /**
     * 全部列表
     * @param $user_id
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getAllHouseMessage($params) {
        //阅读
        db("message_user_house")->where('user_id', $params['user_id'])->update(['is_read' => 1]);
        $data = Db::name("message_user_house")
            ->where("user_id", $params['user_id'])
            ->order('itime', 'desc')
            ->field('title,itime,id')
            ->select();
        foreach ($data as $k => $v) {
            $data[$k]['name'] = '智能选房';
            $data[$k]['itime'] = date('m月d日 H:i', $data[$k]['itime']);
        }
        return $data;
    }

    /**
     * 获取单个
     * @param $id
     * @return array|PDOStatement|string|Model|null
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function gethouse($id) {
        return Db::name('message_user_house')->where('id', $id)->value('house');
    }

}
